package com.ibk.system.login;

import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ibk.system.R;
import com.ibk.system.util.Login;

public class PopViewLayout extends RelativeLayout implements OnClickListener {
	public static final int passViewID = 0x33333333;
	public static final int idViewID = 0x44444444;
	public static final int LOGIN_BUTTON = 100;

	private EditText idView;
	private EditText passView;

	private Button submit;

	private TextView idText;
	private TextView passText;

	public RelativeLayout bodyLayout;
	public PopViewActivity superActivity;

	public PopViewLayout(Context context, PopViewActivity activity) {
		super(context);
		this.setOnClickListener(this);
		this.setBackgroundResource(R.drawable.a00100_b);
		superActivity = activity;

		idText = new TextView(getContext());
		idView = new EditText(getContext());

		passText = new TextView(getContext());
		passView = new EditText(getContext());

		idView.setId(idViewID);
		passView.setId(passViewID);
		
		submit = new Button(getContext());
		submit.setId(LOGIN_BUTTON);
		submit.setOnClickListener(this);

		// idText.setText("  id : ");
		// passText.setText("pass : ");
		submit.setText("Login");

		LayoutParams passViewParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		passViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		passView.setLayoutParams(passViewParams);
		
		LayoutParams passTextParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		passTextParams.addRule(RelativeLayout.LEFT_OF, passView.getId());
		passTextParams.addRule(RelativeLayout.ALIGN_BASELINE, passView.getId());
		passText.setLayoutParams(passTextParams);
		
		LayoutParams idViewParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		idViewParams.addRule(RelativeLayout.ABOVE, passView.getId());
		idViewParams.addRule(RelativeLayout.ALIGN_LEFT, passView.getId());
		idView.setLayoutParams(idViewParams);
		
		LayoutParams idTextParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		idTextParams.addRule(RelativeLayout.LEFT_OF, idView.getId());
		idTextParams.addRule(RelativeLayout.ABOVE, passView.getId());
		idTextParams.addRule(RelativeLayout.ALIGN_BASELINE, idView.getId());
		idText.setLayoutParams(idTextParams);
		
		LayoutParams submitButtonParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		submitButtonParams.addRule(RelativeLayout.BELOW, passView.getId());
		submitButtonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
		submit.setLayoutParams(submitButtonParams);

		bodyLayout = new RelativeLayout(getContext());
		bodyLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		bodyLayout.addView(passView);
		bodyLayout.addView(passText);
		bodyLayout.addView(idView);
		bodyLayout.addView(idText);
		bodyLayout.addView(submit);

		RelativeLayout.LayoutParams item_img_param = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		item_img_param.addRule(RelativeLayout.CENTER_IN_PARENT);
		this.setLayoutParams(item_img_param);
		this.addView(bodyLayout);
	}

	public void onClick(View v) {
		if (v.getId() == LOGIN_BUTTON) {
			String result = Login.login(idView.getText().toString(), passView
					.getText().toString());
			superActivity.nextPage(result);
		} else {
			this.setVisibility(View.INVISIBLE);
			superActivity.popViewVisible = !superActivity.popViewVisible;
		}
	}

	public void setPosition(Display display) {
		int bodyWidth = display.getWidth() / 2;
		// idText.setWidth(50);
		idText.setBackgroundResource(R.drawable.a00700_b);
		// passText.setWidth(50);
		passText.setBackgroundResource(R.drawable.a00800_b);
		idView.setWidth(bodyWidth);
		passView.setWidth(bodyWidth);
		submit.setWidth(bodyWidth / 3);
		// this.setPadding((display.getWidth() - bodyWidth - 50) / 2,
		// (display.getHeight() / 5), 0, 0);
	}

	public void clearComponent() {
		idView.setText("");
		passView.setText("");
	}
}
